import { ChannelType } from '@/store/reducer/channel';
import { Auth } from '@/store/reducer/user';

const TOKEN_KEY = 'geek-park-h5';
const CHANNEL_KEY = 'geek-park-channel';
//获取token
export const getToken = (): Auth => {
  return JSON.parse(localStorage.getItem(TOKEN_KEY) || '{}');
};
//设置token
export const setToken = (tokenInfo: Auth): void => {
  localStorage.setItem(TOKEN_KEY, JSON.stringify(tokenInfo));
};
//移出token
export const removeToken = (): void => {
  localStorage.removeItem(TOKEN_KEY);
};
//判断是否有token
export const hasToken = (): boolean => {
  return !!getToken().token;
};

//获取频道
export const getChannel = (): ChannelType[] => {
  return JSON.parse(localStorage.getItem(CHANNEL_KEY) || '[]');
};
//存频道
export const setChannel = (channelList: ChannelType[]) => {
  localStorage.setItem(CHANNEL_KEY, JSON.stringify(channelList));
};
//删除频道
export const removeChannel = () => {
  localStorage.removeItem(CHANNEL_KEY);
};

const HISTORY_SEARCH = 'history-search';

export const setHistories = (list: string[]) => {
  localStorage.setItem(HISTORY_SEARCH, JSON.stringify(list));
};

export const getHistories = (): string[] => {
  return JSON.parse(localStorage.getItem(HISTORY_SEARCH) || '[]');
};

export const removeHistories = () => {
  localStorage.removeItem(HISTORY_SEARCH);
};
